#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/socket.h>
#include <pthread.h>
 /*
a1Sz1GeUhtv.iot-as-mqtt.cn-shanghai.aliyuncs.com:1883
123456|securemode=3,signmethod=hmacsha1,timestamp=123|
ESP32_note&a1Sz1GeUhtv
775978881356F41677F12D400350729BA09EB131
clientId123456deviceNameESP32_noteproductKeya1Sz1GeUhtvtimestamp123
 */
void my_sprintf(char *dst,const char *src,int size)
{
	int i = 0;
	for(i=0; i<size; i++)
	{
		dst[i] = src[i];
	}
}

int tcpsock = 0;
void *recvData(void *arg)
{
	int ret = 0, i;
	//int socket = *((int *)arg);
while(1)
{
	unsigned char buf[2048] = {0};
	memset(buf, 0, sizeof(buf));
	ret = read(tcpsock,buf,4096);
	if(ret <=0)
		break;

	for(i=0; i<ret; i++)
	//{
		printf("%02X ", buf[i]);
		printf("\n");
	//}
}
pthread_exit(NULL);
}


int main()
{
	const char *ip = "101.133.196.109";
	printf("ip: %s\n",ip);	
	
	//1.创建tcp类型的套接字
	tcpsock=socket(AF_INET,SOCK_STREAM,0);
	if(tcpsock==-1)
	{
		perror("创建套接字失败!\n");
		return -1;
	}
	
	//2.设置HTTP服务器的地址    
	struct sockaddr_in girladdr;
	bzero(&girladdr,sizeof(girladdr));
	girladdr.sin_family= AF_INET; 
	girladdr.sin_addr.s_addr=inet_addr(ip); //时间网站的ip
	girladdr.sin_port=htons(1883);//端口
	
	
	//3.连接
	int ret=connect(tcpsock,(struct sockaddr *)(&girladdr),sizeof(girladdr));
	if(ret==-1)
	{
		perror("连接失败!\n");
		return -1;
	}

 /*
a1Sz1GeUhtv.iot-as-mqtt.cn-shanghai.aliyuncs.com:1883
123456|securemode=3,signmethod=hmacsha1,timestamp=123|
ESP32_note&a1Sz1GeUhtv
775978881356F41677F12D400350729BA09EB131
clientId123456deviceNameESP32_noteproductKeya1Sz1GeUhtvtimestamp123
 */	
	//const char data[1024] = {0x10,0x84,0x01,0x00,0x04 ,0x4d ,0x51,0x54 ,0x54 ,0x04 ,0xc2 ,0x00 ,0x64 ,0x31 ,0x32 ,0x33 ,0x34 ,0x35 ,0x36 ,0x7C ,0x73 ,0x65 ,0x63 ,0x75 ,0x72 ,0x65 ,0x6D ,0x6F ,0x64 ,0x65 ,0x3D ,0x33 ,0x2C ,0x73 ,0x69 ,0x67 ,0x6E ,0x6D ,0x65 ,0x74 68 6F 64 3D 68 6D 61 63 73 68 61 31 2C 74 69 6D 65 73 74 61 6D 70 3D 31 32 33 7C 0D 0A 45 53 50 33 32 5F 6E 6F 74 65 26 61 31 53 7A 31 47 65 55 68 74 76 0D 0A 37 37 35 39 37 38 38 38 31 33 35 36 46 34 31 36 37 37 46 31 32 44 34 30 30 33 35 30 37 32 39 42 41 30 39 45 42 31 33 31};
	const char head[2] = {0x10, 0x84};
	const char changeHead[11] = {0x01 ,0x00 ,0x04 ,0x4d ,0x51 ,0x54 ,0x54 ,0x04 ,0xc2 ,0x00 ,0x64};
	const char ID_len[2] = {0x00, 0x36};
	const char *id = "123456|securemode=3,signmethod=hmacsha1,timestamp=123|";
	const char name_Len[2] = {0x00, 0x16};
	const char *name = "ESP32_note&a1Sz1GeUhtv";
	const char passwd_Len[2] = {0x00, 0x28};
	const char *passwd = "775978881356F41677F12D400350729BA09EB131";
	char buf[2048] = {0};
	memcpy(buf, head, 2);
	memcpy(&buf[2], changeHead, 11);
	memcpy(&buf[13], ID_len, 2);
	memcpy(&buf[15], id, strlen(id));
	memcpy(&buf[15+strlen(id)], name_Len, 2);
	memcpy(&buf[15+strlen(id)+2], name, strlen(name));
	memcpy(&buf[15+strlen(id)+2+strlen(name)], passwd_Len, 2);
	memcpy(&buf[15+strlen(id)+2+strlen(name)+2], passwd, strlen(passwd));
	
//	sprintf(buf, "%s%s%s%s%s%s%s%s", head, changeHead, ID_len, id, name_Len, name, passwd_Len, passwd);
	
//	printf("buf = %s\n", buf);


	ret = write(tcpsock,buf,2+11+2+strlen(id)+ 2 + strlen(name)+2+strlen(passwd));
	printf("write = %d\n", ret);	

	int i=0;
	ret = read(tcpsock,buf,4096);
	printf("ret = %d\n", ret);


//	ret = read(tcpsock,buf,4096);
//	printf("len = %d\n", ret);
//return 0;
memset(buf, 0, sizeof(buf));
const char len[6] = {0x30, 0x96, 0x01, 0x00, 0x36, 0x2F};
//const char *post = "sys/a1Sz1GeUhtv/ESP32_note/thing/event/property/post{\"method\":\"thing.event.property.post\",\"id\":\"1376401144\",\"params\":{\"light\":1},\"version\":\"1.0.0\"}";
//memcpy(&buf[6], post, strlen(post));
uint8_t post[0x97+3] = {0x30,0x95,0x01,0x00,0x35,0x2F,0x73,0x79,0x73,0x2F,\
0x61,0x31,0x53,0x7A,0x31,0x47,0x65,0x55,0x68,0x74,\
0x76,0x2F,0x45,0x53,0x50,0x33,0x32,0x5F,0x6E,0x6F,\
0x74,0x65,0x2F,0x74,0x68,0x69,0x6E,0x67,0x2F,0x65,\
0x76,0x65,0x6E,0x74,0x2F,0x70,0x72,0x6F,0x70,0x65,\
0x72,0x74,0x79,0x2F,0x70,0x6F,0x73,0x74,0x7B,0x22,\
0x6D,0x65,0x74,0x68,0x6F,0x64,0x22,0x3A,0x22,0x74,\
0x68,0x69,0x6E,0x67,0x2E,0x65,0x76,0x65,0x6E,0x74,\
0x2E,0x70,0x72,0x6F,0x70,0x65,0x72,0x74,0x79,0x2E,\
0x70,0x6F,0x73,0x74,0x22,0x2C,0x22,0x69,0x64,0x22,\
0x3A,0x22,0x33,0x32,0x30,0x37,0x39,0x38,0x38,0x39,\
0x30,0x22,0x2C,0x22,0x70,0x61,0x72,0x61,0x6D,0x73,\
0x22,0x3A,0x7B,0x22,0x6C,0x69,0x67,0x68,0x74,0x22,\
0x3A,0x31,0x7D,0x2C,0x22,0x76,0x65,0x72,0x73,0x69,\
0x6F,0x6E,0x22,0x3A,0x22,0x31,0x2E,0x30,0x2E,0x30,\
0x22,0x7D};



/*
ret = write(tcpsock,post, 0x97+3);
if(ret <=0)
	perror("write fail\n");
printf("write success\n");
	ret = read(tcpsock,buf,4096);
*/
pthread_t tid = 0;
pthread_create(&tid, NULL, recvData, NULL);
pthread_detach(tid);

int chose = 0;
	while(1)
	{
		scanf("%d", &chose);
		switch(chose)
		{
			case 1:
			{
				ret = write(tcpsock,post, 0x97+3);
				if(ret <=0)
				{
					perror("write fail\n");
					close(tcpsock);
					return 0;	
				}
				printf("write success\n");
			}
			break;
			case 2:
			{
				close(tcpsock);
				printf("return\n");
				return 0;
			}
		}
/*
	unsigned char buf[2048] = {0};
	memset(buf, 0, sizeof(buf));
	ret = read(tcpsock,buf,4096);
	if(ret <=0)
		break;

	for(i=0; i<ret; i++)
	//{
		printf("%02X ", buf[i]);
		printf("\n");
	//}
*/
}
	printf("exit: %d\n", ret);
/*
	//发送HTTP请求协议
	write(tcpsock,cmd,strlen(cmd));
	
	//接收返回的请求信息
	char rbuf[4096]={0};
	read(tcpsock,rbuf,4096);
	printf("获取到的信息:\n%s\n",rbuf);
	
	print_json(strstr(rbuf, "{"));
*/	
	//关闭
	close(tcpsock);
	return 0;
}
